/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Formularios;

import com.thoughtworks.xstream.XStream;
import hispamail.CBackground;
import hispamail.CDir;
import hispamail.CSendmail2;
import hispamail.FileDrop;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;

/**
 *
 * @author MCS
 */
public class JDCorreo extends javax.swing.JDialog {

    /**
     * Creates new form Correo
     */
    private static CBackground background = new CBackground();

    public JDCorreo(java.awt.Frame parent, boolean modal, CBackground back) {
        super(parent, modal);
        JDCorreo.background = back;
        initComponents();
        //Creamos un listener para capturar  los archivos drop en los jTextArea y jTextAdjuntos
        FileDrop fileDrop;//FileDrop.Listener
        fileDrop = new FileDrop(jTextAdjuntos, new FileDrop.Listener() {
            @Override
            public void filesDropped(java.io.File[] files) {
                drop(files);
            }
        });

        fileDrop = new FileDrop(jTextArea, new FileDrop.Listener() {
            @Override
            public void filesDropped(java.io.File[] files) {
                drop(files);
            }
        });

    }

    //manejo del drop de ficheros htm y csv
    private void drop(java.io.File[] files) {
        //Manejamos el archivo drop
        String cad = "";

        for (int i = 0; i < files.length; i++) {
            try {
                if (jTextAdjuntos.getText().length() == 0) {
                    jTextAdjuntos.setText(files[i].toString());
                } else {
                    jTextAdjuntos.setText(jTextAdjuntos.getText() + ";\n" + files[i].getCanonicalPath());
                }
            } catch (java.io.IOException e) {

            }
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton3 = new javax.swing.JButton();
        jTextTO = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jTextCC = new javax.swing.JTextField();
        jTextBCC = new javax.swing.JTextField();
        jButton2 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea = new javax.swing.JTextArea();
        jLabel5 = new javax.swing.JLabel();
        jTextAsunto = new javax.swing.JTextField();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextAdjuntos = new javax.swing.JTextArea();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();

        jButton3.setText("jButton3");

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Correo");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        jButton1.setText("Enviar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Adjuntos");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jTextArea.setColumns(20);
        jTextArea.setFont(new java.awt.Font("Tahoma", 0, 13)); // NOI18N
        jTextArea.setRows(5);
        jScrollPane1.setViewportView(jTextArea);

        jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel5.setText("Asunto:");

        jTextAdjuntos.setColumns(20);
        jTextAdjuntos.setFont(new java.awt.Font("Tahoma", 0, 13)); // NOI18N
        jTextAdjuntos.setRows(5);
        jScrollPane2.setViewportView(jTextAdjuntos);

        jButton4.setText("TO");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jButton5.setText("CC");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jButton6.setText("BCC");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 729, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addGap(13, 13, 13)
                                .addComponent(jButton2)
                                .addGap(6, 6, 6)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jTextAsunto)
                                    .addComponent(jTextBCC, javax.swing.GroupLayout.DEFAULT_SIZE, 638, Short.MAX_VALUE)
                                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 638, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addGap(18, 18, 18)
                        .addComponent(jButton1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(27, 27, 27)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                                    .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jButton6))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jTextTO, javax.swing.GroupLayout.PREFERRED_SIZE, 638, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jTextCC, javax.swing.GroupLayout.PREFERRED_SIZE, 638, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addComponent(jButton5)
                            .addComponent(jButton4))))
                .addContainerGap(30, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jTextAsunto, jTextBCC});

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jButton4, jButton5, jButton6});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextTO, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextCC, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(10, 10, 10)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextBCC, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jTextAsunto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton2)
                        .addGap(265, 265, 265))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 247, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jButton1))
                        .addGap(22, 22, 22))))
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jButton4, jButton5, jButton6});

        getAccessibleContext().setAccessibleName("JDCorreo");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        //forma paquete de envio con Xstream y lo graba en pendientes
        System.out.println("Adjuntos:");
        System.out.println(jTextAdjuntos.getText());

        //generamos el correo serializado a partir de CSendmail2
        CSendmail2 correo = new CSendmail2(jTextTO.getText(),
                jTextCC.getText(),
                jTextBCC.getText(),
                jTextAsunto.getText(),
                jTextAdjuntos.getText(),
                jTextArea.getText());

        //guardamos el registro de libreta con xstream
        XStream xstream = new XStream();
        xstream.alias("correo", CSendmail2.class);
        FileOutputStream flop;
        try {
            flop = new FileOutputStream(JFrameInicio.pendientes + correo.getPak());
            xstream.toXML(correo, flop);
            flop.close();

        } catch (FileNotFoundException ex) {
            Logger.getLogger(JLibreta.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(JLibreta.class.getName()).log(Level.SEVERE, null, ex);
        }

        background.setSuspend(false);
        dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
        background.setSuspend(false);
    }//GEN-LAST:event_formWindowClosed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        JFileChooser chooser = new JFileChooser();

        chooser.setMultiSelectionEnabled(true);

        int returnVal;
        returnVal = chooser.showOpenDialog(null);
        File[] files = chooser.getSelectedFiles();
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            for (int i = 0; i < files.length; i++) {
                if (jTextAdjuntos.getText().length() == 0) {
                    jTextAdjuntos.setText(files[i].toString());
                } else {
                    jTextAdjuntos.setText(jTextAdjuntos.getText() + ";\n" + files[i].toString());
                }
            }

        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        ArrayList<CDir> Correos = new ArrayList<>();

        JLibreta pantalla = new JLibreta(null, true, Correos);
        pantalla.setLocationRelativeTo(null);
        pantalla.setVisible(true);

        //Al salir de la libreta añado las direcciones seleccionadas a las que ya hubiera
        String cad = jTextTO.getText().toString();
        for (int i = 0; i < Correos.size(); i++) {
            if (cad.length() > 0) {
                cad = cad.concat(";" + Correos.get(i).getCorreo());
            } else {
                cad = cad.concat(Correos.get(i).getCorreo());
            }
        }
        jTextTO.setText(cad);
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        ArrayList<CDir> Correos = new ArrayList<>();

        JLibreta pantalla = new JLibreta(null, true, Correos);
        pantalla.setLocationRelativeTo(null);
        pantalla.setVisible(true);

        //Al salir de la libreta añado las direcciones seleccionadas a las que ya hubiera
        String cad = jTextCC.getText().toString();
        for (int i = 0; i < Correos.size(); i++) {
            if (cad.length() > 0) {
                cad = cad.concat(";" + Correos.get(i).getCorreo());
            } else {
                cad = cad.concat(Correos.get(i).getCorreo());
            }
        }
        jTextCC.setText(cad);
    }//GEN-LAST:event_jButton5ActionPerformed

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
        ArrayList<CDir> Correos = new ArrayList<>();

        JLibreta pantalla = new JLibreta(null, true, Correos);
        pantalla.setLocationRelativeTo(null);
        pantalla.setVisible(true);
        
        //Al salir de la libreta añado las direcciones seleccionadas a las que ya hubiera
        String cad = jTextBCC.getText().toString();
        for (int i = 0; i < Correos.size(); i++) {
            if (cad.length() > 0) {
                cad = cad.concat(";" + Correos.get(i).getCorreo());
            } else {
                cad = cad.concat(Correos.get(i).getCorreo());
            }
        }
        jTextBCC.setText(cad);
    }//GEN-LAST:event_jButton6ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(JDCorreo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                JDCorreo dialog = new JDCorreo(new javax.swing.JFrame(), true, background);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea jTextAdjuntos;
    private javax.swing.JTextArea jTextArea;
    private javax.swing.JTextField jTextAsunto;
    private javax.swing.JTextField jTextBCC;
    private javax.swing.JTextField jTextCC;
    private javax.swing.JTextField jTextTO;
    // End of variables declaration//GEN-END:variables
}
